package com.isoft.filelistener.flistener;

import com.isoft.filelistener.Utils.ApplicationContextProvider;
import com.isoft.filelistener.Utils.XmlUtils;
import com.isoft.filelistener.entity.*;
import com.isoft.filelistener.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

@Component
@Slf4j
public class FListener implements FileAlterationListener {
    private  String driction=null;
    @Override
    public void onDirectoryChange(File file) {
        log.info("文件夹修改...");
    }

    @Override
    public void onDirectoryCreate(File file) {
        log.info("文件夹创建..."+file.getName());
        driction=file.getName();
    }
    @Override
    public void onDirectoryDelete(File file) {
        log.info("文件夹删除...");
    }

    @Override
    public void onFileChange(File file) {
        log.info("文件修改..."+file.getName());
    }

    @Override
    public void onFileCreate(File file) {
        String name = file.getName();
        switch (name.substring(0,9)){
            case "BASE-APUE":
                try {
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    BaseApue baseApue= XmlUtils.parseXml(inputStream, BaseApue.class);
                    ApplicationContextProvider.getBean(BaseApueService.class).InsertBaseApue(baseApue.getMetaData(),baseApue.getFlight());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "BASE-CFUE":
                try {
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    BaseCfue baseCfue=XmlUtils.parseXml(inputStream, BaseCfue.class);
                    ApplicationContextProvider.getBean(BaseCfueService.class).InsertBaseCfue(baseCfue.getMetaData(),baseCfue.getCfue());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-AFID":
                try {
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    DfmeAfid dfmeairl= XmlUtils.parseXml(inputStream, DfmeAfid.class);
                    ApplicationContextProvider.getBean(DfmeAfidService.class).InsertDfmeAfid(dfmeairl.getMetaData(),dfmeairl.getAfid());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-AIRL":
                try {
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    DfmeAIRL dfmeairl= XmlUtils.parseXml(inputStream, DfmeAIRL.class);
                    DfmeAirlApno apno = dfmeairl.getDflt().getApnos().get(0);
                    DfmeAirlApno apno1 = dfmeairl.getDflt().getApnos().get(1);
                    ApplicationContextProvider.getBean(DfmeAirlService.class).InsertDfmeAfid(dfmeairl.getMetaData(),dfmeairl.getDflt(),apno,apno1);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-ARRE":
                try {
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                    DfmeArre dfmeArre= XmlUtils.parseXml(inputStream, DfmeArre.class);
                    ApplicationContextProvider.getBean(DfmeArreService.class).InsertDfmeArre(dfmeArre.getMetaData(),dfmeArre.getDflt());
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            case "DFME-BLLS":
                try {
                    InputStream inputStream=new FileInputStream("D:\\files\\"+driction+"\\"+file.getName());
                   DfmeBlls dfmeBlls = XmlUtils.parseXml(inputStream, DfmeBlls.class);
                   log.info(dfmeBlls+"");
                   DfmeBllsdfltson dfmeBllsdfltson = dfmeBlls.getDflt().getDfltBlls().get(0);
                   ApplicationContextProvider.getBean(DfmeBllsService.class).InsertDfmeBlls(dfmeBlls.getMetaData(),dfmeBlls.getDflt(),dfmeBllsdfltson);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                break;
            default:
                log.info("没有此文件");
        }
    }
    @Override
    public void onFileDelete(File file) {
        log.info("文件删除..."+file.getName());
    }

    @Override
    public void onStart(FileAlterationObserver fileAlterationObserver) {
        log.info("监听开始4.....");
    }

    @Override
    public void onStop(FileAlterationObserver fileAlterationObserver) {
        log.info("监听结束4.....");
    }
}
